package com.huangpuweb.task.controller;

import com.alibaba.druid.sql.dialect.mysql.visitor.transform.NameResolveVisitor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huangpuweb.task.model.LxmReXian;
import com.huangpuweb.task.model.SysXiaoGongCheng;
import com.huangpuweb.task.service.LaoXiMenApiService;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.lang.annotation.ElementType;
import java.util.List;

/**
 * @author Administrator
 */
@Controller
public class QueryReXianDataJob {
    @Resource
    private LaoXiMenApiService laoXiMenApiService;

    public void QueryReXianDataTask() {
        LogFactory.getLog(this.getClass()).info("QueryReXianDataTask------------------start");
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://172.21.186.204/StreetTagInfo/QueryReXianData?rec=ReXianData");
            post.setHeader("Content-Type", "application/json;charset=utf-8");
            HttpResponse execute = httpClient.execute(post);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            LxmReXian lxmReXian = new LxmReXian();
            JSONArray jsonArray = JSON.parseArray(entity);
            if (!jsonArray.isEmpty()) {
                laoXiMenApiService.truncateTable();
            }
            for (int i = 0; i < jsonArray.size(); i++) {
                lxmReXian.setID(Integer.valueOf(jsonArray.getJSONObject(i).getString("ID")));
                lxmReXian.setAJZT(jsonArray.getJSONObject(i).getString("AJZT"));
                lxmReXian.setRWH(jsonArray.getJSONObject(i).getString("RWH"));
                lxmReXian.setFXSJ(jsonArray.getJSONObject(i).getString("FXSJ"));
                lxmReXian.setWTLX(jsonArray.getJSONObject(i).getString("WTLX"));
                lxmReXian.setStreet(jsonArray.getJSONObject(i).getString("Street"));
                lxmReXian.setJuWei(jsonArray.getJSONObject(i).getString("JuWei"));
                lxmReXian.setFSDZ(jsonArray.getJSONObject(i).getString("FSDZ"));
                lxmReXian.setSQLXR(jsonArray.getJSONObject(i).getString("SQLXR"));
                lxmReXian.setLXFS(jsonArray.getJSONObject(i).getString("LXFS"));
                lxmReXian.setWTMS(jsonArray.getJSONObject(i).getString("WTMS"));
                lxmReXian.setZZBM(jsonArray.getJSONObject(i).getString("ZZBM"));
                lxmReXian.setSJZZBM(jsonArray.getJSONObject(i).getString("SJZZBM"));
                lxmReXian.setJASJ(jsonArray.getJSONObject(i).getString("JASJ"));
                lxmReXian.setFKJL(jsonArray.getJSONObject(i).getString("FKJL"));
                lxmReXian.setMYQK(jsonArray.getJSONObject(i).getString("MYQK"));
                lxmReXian.setCQQK(jsonArray.getJSONObject(i).getString("CQQK"));
                lxmReXian.setDBQK(jsonArray.getJSONObject(i).getString("DBQK"));
                lxmReXian.setObjectEntryState(Integer.valueOf(jsonArray.getJSONObject(i).getString("ObjectEntryState")));
                lxmReXian.setNotMappedProperties(jsonArray.getJSONObject(i).getString("NotMappedProperties"));
                laoXiMenApiService.insertReXianData(lxmReXian);
            }
            LogFactory.getLog(this.getClass()).info("QueryReXianDataTask------------------end");
        } catch (Exception e) {
            LogFactory.getLog(this.getClass()).info("QueryReXianDataTask------------------error" + e);
        }
    }

    public void xiaoGongChengTask() {
        LogFactory.getLog(this.getClass()).info("xiaoGongChengTask------------------start");
        try {
            HttpClient httpClient = new DefaultHttpClient();
            //172.82.4.51     173.82.57.48
            HttpGet get = new HttpGet("http://172.82.4.51:8082/wx/webGetGongdi_AllBySortDate");
            HttpResponse execute = httpClient.execute(get);
            String entity = EntityUtils.toString(execute.getEntity(), "utf-8");
            SysXiaoGongCheng xc = new SysXiaoGongCheng();
            JSONObject jsonObject = JSON.parseObject(entity);
            if (jsonObject != null) {
                laoXiMenApiService.truncateGongChengTable();
            }
            xc.setJanuary(JSONObject.parseObject(jsonObject.getString("state")).getString("01"));
            xc.setFebruary(JSONObject.parseObject(jsonObject.getString("state")).getString("02"));
            xc.setMarch(JSONObject.parseObject(jsonObject.getString("state")).getString("03"));
            xc.setApr(JSONObject.parseObject(jsonObject.getString("state")).getString("04"));
            xc.setMay(JSONObject.parseObject(jsonObject.getString("state")).getString("05"));
            xc.setJune(JSONObject.parseObject(jsonObject.getString("state")).getString("06"));
            xc.setJuly(JSONObject.parseObject(jsonObject.getString("state")).getString("07"));
            xc.setAugust(JSONObject.parseObject(jsonObject.getString("state")).getString("08"));
            xc.setSeptember(JSONObject.parseObject(jsonObject.getString("state")).getString("09"));
            xc.setOctober(JSONObject.parseObject(jsonObject.getString("state")).getString("10"));
            xc.setNovember(JSONObject.parseObject(jsonObject.getString("state")).getString("11"));
            xc.setDecember(JSONObject.parseObject(jsonObject.getString("state")).getString("12"));
            JSONArray projects = JSONArray.parseArray(jsonObject.getString("projects"));
            for (int i = 0; i < projects.size(); i++) {
                //痛苦的程序猿
                xc.set_id(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("_id") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("_id"));
                xc.setState(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("state") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("state"));
                xc.setRefuse_text(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("refuse_text") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("refuse_text"));
                xc.setPrj_name(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_name") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_name"));
                xc.setPrj_addr(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_addr") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_addr"));
                xc.setPrj_area(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_area") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_area"));
                xc.setPrj_price(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_price") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_price"));
                xc.setDemand_com(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("demand_com") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("demand_com"));
                xc.setConstruction_com(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("construction_com") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("construction_com"));
                xc.setPrj_state(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_state") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_state"));
                xc.setCheck_depart(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_depart") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_depart"));
                xc.setCheck_person(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_person") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_person"));
                xc.setCheck_content(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_content") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_content"));
                xc.setCheck_question(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_question") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_question"));
                xc.setCheck_question_desp(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_question_desp") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("check_question_desp"));
                xc.setChange_state(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("change_state") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("change_state"));
                xc.setChange_desp(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("change_desp") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("change_desp"));
                xc.setChange_pictures(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("change_pictures") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("change_pictures"));
                xc.setPrj_property(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_property") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_property"));
                xc.setPrj_lease_contract(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_lease_contract") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_lease_contract"));
                xc.setPrj_con_contract(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_con_contract") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_con_contract"));
                xc.setPrj_license(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_license") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_license"));
                xc.setPrj_certifications(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_certifications") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_certifications"));
                xc.setPrj_manager_cert(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_manager_cert") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_manager_cert"));
                xc.setPrj_safe_cert(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_safe_cert") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_safe_cert"));
                xc.setPrj_manager_appiontment(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_manager_appiontment") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_manager_appiontment"));
                xc.setPrj_safe_appiontment(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_safe_appiontment") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_safe_appiontment"));
                xc.setPrj_design_cert(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_design_cert") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_design_cert"));
                xc.setPrj_assist_org(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_assist_org") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_assist_org"));
                xc.setPrj_person_name(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_person_name") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_person_name"));
                xc.setPrj_person_phone(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_person_phone") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_person_phone"));
                xc.setPrj_type(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_type") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_type"));
                xc.setStart_date(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("start_date") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("start_date"));
                xc.setUserid(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("userid") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("userid"));
                xc.setUpdateTime(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("updateTime") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("updateTime"));
                xc.set__v(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("__v") == null ? "" : JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("__v"));
                if (JSONObject.parseObject(projects.getString(i)).getString("prj_grid") != null) {
                    xc.setPrj_grid(JSONObject.parseObject(projects.getString(i)).getString("prj_grid"));
                } else if (JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_grid") != null) {
                    xc.setPrj_grid(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_grid"));
                } else {
                    xc.setPrj_grid("");
                }
                if (JSONObject.parseObject(projects.getString(i)).getString("picture") != null) {
                    xc.setPicture(JSONObject.parseObject(projects.getString(i)).getString("picture"));
                } else if (JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("picture") != null) {
                    xc.setPicture(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("picture"));
                } else {
                    xc.setPicture("");
                }
                if (JSONObject.parseObject(projects.getString(i)).getString("location") != null) {
                    xc.setLocation(JSONObject.parseObject(projects.getString(i)).getString("location"));

                } else if (JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("location") != null) {
                    xc.setLocation(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("location"));
                } else {
                    xc.setLocation("");
                }
                if (JSONObject.parseObject(projects.getString(i)).getString("prj_depart") != null) {
                    xc.setPrj_depart(JSONObject.parseObject(projects.getString(i)).getString("prj_depart"));
                } else if (JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_depart") != null) {
                    xc.setPrj_depart(JSONObject.parseObject(JSONObject.parseObject(projects.getString(i)).getString("data")).getString("prj_depart"));
                } else {
                    xc.setPrj_depart("");
                }
                laoXiMenApiService.insertXiaoGongCheng(xc);
            }

            LogFactory.getLog(this.getClass()).info("xiaoGongChengTask------------------end");
        } catch (Exception e) {
            e.printStackTrace();
//            LogFactory.getLog(this.getClass()).info("xiaoGongChengTask------------------error" + e);
        }
    }
}
